package dome5;

import java.util.Scanner;

public class Day0203 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int num = in.nextInt();
        String[] arr = new String[num];
        String str1 = in.next();
        String str2 = in.next();
        for(int i = 0; i < num; i++) {
            arr[i] = in.next();
        }
        int a = -1;
        int b = -1;
        int count = 0;
        int min = num;
        for(int i = 0; i < num; i++) {
            if(arr[i].equals(str1)) {
                a = i ;
            }
            if(arr[i].equals(str2)) {
                b = i;
            }
            if(a > 0 && b > 0) {
                count = Math.abs(a-b);
                if(min > count) {
                    min = count;
                }
            }
        }
        if(a < 0 || b < 0) {
            System.out.println(-1);
            return;
        }
        System.out.println(min);
    }
}
